/**
 * Copyright 2019 [shujiang.yang], Inc. All rights reserved.
 * Email: 314938230@qq.com
**/
define("pages/snjy/snjy_doc", [
        "../../common/componts.js",
        "../../plugins/easyui/jquery-easyui-1.5.1/jquery.easyui.min",
        "../../plugins/easyui/jquery-easyui-1.5.1/easyloader",
        "../../plugins/artTemplate/artTemplate"
    ],
    function(require) {
        var componts = require("../../common/componts");
        var queryTableId="awdDocTableId";
        var selectid;

        var getQueryDataGrid = function(){
            return $("#"+queryTableId);
        };

        var tableInit=function(){
            componts.initTable(queryTableId,{
                url:"/snjy/list",
                title:'',
                fit:true,
                // firstLoad:true,
                width:'100%',
                columns :  [
                    {
                        field: 'id',
                        title: '序号',
                        width:30,
                        formatter: function(value, rowData, rowIndex){
                            return '<input type="radio" name="ryRadio" id="ryRadio' + rowIndex + '" value="' + rowData.id + '" />';
                        }}
            ,{field:'jsbh',title:'拘室号',width:20,sortable:'true',formatter: function(value){return value?'<span title="'+value+'">'+value+'</span>':'';}}
            ,{field:'xm',title:'姓名',width:20,sortable:'true',formatter: function(value){return value?'<span title="'+value+'">'+value+'</span>':'';}}
            ,{field:'xbString',title:'性别',width:20,sortable:'true',formatter: function(value){return value?'<span title="'+value+'">'+value+'</span>':'';}}
            ,{field:'ysString',title:'入所时间',width:20,sortable:'true',formatter: function(value){return value?'<span title="'+value+'">'+value+'</span>':'';}}
            ,{field:'rybh',title:'档案编号',width:20,sortable:'true',formatter: function(value){return value?'<span title="'+value+'">'+value+'</span>':'';}}
            ,{field:'ayString',title:'案别',width:20,sortable:'true',formatter: function(value){return value?'<span title="'+value+'">'+value+'</span>':'';}}
            ,{field:'hjdString',title:'户籍所在地',width:20,sortable:'true',formatter: function(value){return value?'<span title="'+value+'">'+value+'</span>':'';}}
            ,{field:'zz',title:'症状',width:20,sortable:'true',formatter: function(value){return value?'<span title="'+value+'">'+value+'</span>':'';}}
            ,{field:'tz',title:'体征',width:20,sortable:'true',formatter: function(value){return value?'<span title="'+value+'">'+value+'</span>':'';}}
            ,{field:'jcjg',title:'检查结果',width:20,sortable:'true',formatter: function(value){return value?'<span title="'+value+'">'+value+'</span>':'';}}
            ,{field:'zzjg',title:'诊治结果',width:20,sortable:'true',formatter: function(value){return value?'<span title="'+value+'">'+value+'</span>':'';}}
            ,{field:'ys',title:'诊治医生',width:20,sortable:'true',formatter: function(value){return value?'<span title="'+value+'">'+value+'</span>':'';}}
            ,{field:'zdrq',title:'诊治日期',width:20,sortable:'true',formatter: function(value){return value?'<span title="'+value+'">'+value+'</span>':'';}}
            ,{field:'operate',title:'详细信息',align:'center',width : 60,
                formatter:function(value, row, index){
                return '<a href="#" data-target="#ckPhoto" value="'+index+'" data-toggle="modal" class="easyui-linkbutton awd-doc-view-detail"><img src="../../img/xq.png"></a>';
            }}
            ] ,
            //基本信息双击事件
            onDblClickRow:function(rowIndex, rowData){
                $("input[name='ryRadio']")[rowIndex].checked = true;
                getQueryDataGrid().datagrid("selectRow", rowIndex);
                selectid=rowData.id;
            },
            onLoadSuccess:function(data){
                if(data.total>0){
                getQueryDataGrid().datagrid("selectRow", 0);
                $("input[name='ryRadio']")[0].checked = true;
            }
                //$("a[name='opera']").each(function(){
                //    $(this).on('click',function () {
                //        var index = $(this).attr('value');
                //        var jbxx = data.rows[index];
                //        kss.initJbxxxx(jbxx.gcbh,jbxx.rybh,jbxx.xm+"--详细信息");
                //    });
                //});
            },
            //单击行选中取消事件
            onClickRow:function(rowIndex, rowData){
                if(selectid!=rowData.id){
                $("input[name='ryRadio']")[rowIndex].checked = true;
                selectid=rowData.id;
            }else{
                getQueryDataGrid().datagrid("unselectRow",rowIndex);
                $("input[name='ryRadio']")[rowIndex].checked = false;
                getQueryDataGrid().datagrid('clearSelections');
                selectid="";
            }
            }
            });
            //页面高度自适应
            getQueryDataGrid().datagrid('resize',{
                height:($(window).height()-150)
            });
            };

            /**
             * 数据列表刷新加载
             */
            var loadDataGrid = function(){
                var formData = $(".awd-doc-form").serializeObject();
               // var queryParams = $.extend({},formData,{"state":"R8"});
                var queryParams = $.extend({},formData,{
                    "processDefinitionKey":componts.getUrlParam("processDefinitionKey"),
                    "taskDefinitionKey":componts.getUrlParam("taskDefinitionKey"),
                    "pageSize":function () {
                        return getQueryDataGrid().datagrid("getPager").pagination("options").pageSize;
                        return getQueryDataGrid().datagrid("getPager").pagination("options").pageSize;
                    },
                    "pageIndex":function () {
                        return getQueryDataGrid().datagrid("getPager").pagination("options").pageNumber;
                    }
                });
                getQueryDataGrid().datagrid('clearSelections');
                getQueryDataGrid().datagrid('load',queryParams);
            };

        var bindAction=function(){
            //查询
            $(".awd-doc-query-btn").on("click",function(){
                loadDataGrid();
            });
            //管理文书
            $(".manage-doc-btn").on("click",function(){
                alert("管理文书")
            });
            //导出Excel
            $(".awd-doc-export-btn").on("click",function(){
                getQueryDataGrid().datagrid('toExcel',{"filename":"所内就医.xls"});
            });

            //单击行详情
            $(document).on('click','.awd-doc-view-detail',function(){
                console.log('awd-doc-view-detail',$(this).attr("value"));
            });

            /**
             * 绑定回车事件
             */
            $(window).keydown(function(event) {
                if (event.keyCode == 13) {
                    loadDataGrid();
                }
            });

        };

        //初始化
        (function(){
            componts.initSelectPage(undefined);
            componts.initSelect(undefined,undefined,undefined);
            tableInit();
            bindAction();
        })()
});
